<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css">
<link rel="stylesheet" href="static/css/style.css" />
<link rel="stylesheet" href="static/css/tcommon.css" />
<script src="static/jquery/jquery-1.12.4.js"></script>
<script src="static/easyui/jquery.easyui.min.js"></script>
<script src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>技术论坛</title>
</head>

<body>
	<div class="content-container" data-options="region:'center'" title="技术论坛列表">
		<div class="panel-header">
			<div class="panel-title">帖子列表</div>
		</div>
		<table id="pro_table" class="easyui-datagrid" style="height: 100%">
		</table>

		<div id="pro_tb">
			<div class="search-input">
				查看：
				<select id="hightlight" class="easyui-combobox" name="hightlight" style="width: 120px;" editable="false"
					data-options="
							    onSelect: function(rec){
							   		hightlight=rec.value;
							   		if(isnew){
							   			isnew = false;
							   		}else{
							   			refreshProTable();
							   		}
							    },panelHeight: 'auto'">
					<option value="">最新</option>
					<option value="2">精品</option>
				</select>
			</div>
			<div class="search-input">
				发布人：
				<input id="name" class="easyui-textbox" />
			</div>
			<div class="search-input">
				帖子主题：
				<input id="title" class="easyui-textbox" />
			</div>
			<div class="search-input">
				<a href="#" class="easyui-linkbutton" iconCls="icon-search" id="search">搜索</a>
			</div>
			<div>
				<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">发表新帖</a>
				<a href="javascript:void(0)" class="d-btn btn-yellow" onclick="myforum()">我的帖子</a>
			</div>
		</div>
	</div>
	<div id="proDlg" class="easyui-dialog info_dialog" style="width: 800px; padding: 10px 20px; top: 30px;"
		closed="true" buttons="#pro-dlg-buttons">
		<form id="proFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">标题：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="title1" name="title1"
								style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">内容：</td>
						<td colspan="3">
							<input class="easyui-textbox" data-options="required:true,multiline:true" id="content" name="content"
								style="width: 550px; height: 100px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">上传图片：</td>
						<td>
							<input class="easyui-filebox"
								data-options="prompt:'添加文件', buttonText:'选择附件',multiple:true,accept:'image/*'" id="images"
								name="images" style="width: 300px;" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons">
		<a href="# " class="easyui-linkbutton ok" onclick="savePro()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg').dialog('close')">关闭</a>
	</div>
</body>
<script>
	var name = "";
	var title = "";
	var hightlight = "";
	var isnew = true;
	$(document).ready(function() {
		$('#pro_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#pro_tb',
			url : "forumPosts",
			method : 'get',
			queryParams : {
				status : 2,
				hightlight : hightlight,
				name : name,
				title : title
			},
			onSelect : function(index,row) {
				currentPro = row;
			},
			columns : [ [ {
				field : 'title',
				title : '帖子主题',
				width : '',
				formatter : function(value,row,index) {
					return '<a href="#" onclick="toreply(' + index + ')">'+value+'</a>'
				}
			},{
				field : 'name',
				title : '作者',
				width : '',
				formatter : function(value,row,index) {
					if (row.user != null){
						return '<a href="#" onclick="toperson(' + index + ')">'+row.user.name+'</a>'
					}else{
						return "";
					}
				}
			},{
				field : 'replycount',
				title : '回复',
				width : ''
			},{
				field : 'updatetime',
				title : '最后发表',
				width : '',
				formatter : function(value,row,index) {
					if (row.updatetime == null){
						return formatDate(row.createdate);
					}else{
						return formatDate(row.updatetime);
					}
				}
			} ] ]
		});
	});
	$("#search").click(function() {
		title = $("#title").textbox('getValue');
		name = $("#name").textbox('getValue');
		refreshProTable();
	});
	function refreshProTable() {
		$('#pro_table').datagrid("load", {
			status : 2,
			title : title,
			name : name,
			hightlight : hightlight
		});
	}
	function newpro() {
		$("#proDlg").dialog('open').dialog('setTitle', '发表新帖');
		$("#proFm").form('clear');
	}
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		var formData = new FormData();
		formData.append("title", $("#title1").textbox('getValue'));
		formData.append("content", $("#content").textbox('getValue'));
		var images = document.getElementById("filebox_file_id_1").files;
		for(var i = 0;i < images.length;i++){
			formData.append('images', images[i]);
		}
		$.ajax({
			type : 'POST',
			url : "forumPostSave.do",
			cache : false,
			data : formData,
			processData : false,
			contentType : false,
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '发表新帖',
						msg : '发表新帖成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshProTable();
				}else{
					$.messager.alert('发表新帖', '发表新帖失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function myforum() {
		$('#pro_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#pro_tb',
			url : "forumPosts",
			method : 'get',
			queryParams : {
				flag : true,
			},
			onSelect : function(index,row) {
				currentPro = row;
			},
			columns : [ [ {
				field : 'title',
				title : '帖子主题',
				width : '',
				formatter : function(value,row,index) {
					return '<a href="#" onclick="toreply(' + index + ')">' + value + '</a>'
				}
			},{
				field : 'name',
				title : '作者',
				width : '',
				formatter : function(value,row,index) {
					if (row.user != null){
						return '<a href="#" onclick="toperson(' + index + ')">' + row.user.name + '</a>'
					}else{
						return "";
					}
				}
			},{
				field : 'replycount',
				title : '回复',
				width : ''
			},{
				field : 'updatetime',
				title : '最后发表',
				width : '',
				formatter : function(value,row,index) {
					if (row.updatetime == null){
						return formatDate(row.createdate);
					}else{
						return formatDate(row.updatetime);
					}
				}
			},{
				field : 'status',
				title : '状态',
				width : '',
				formatter : function(value,row,index) {
					if (value == 1){
						return "待审";
					}else{
						return "已审";
					}
				}
			} ] ]
		});
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear();
		var month = date.getMonth() + 1; //月份是从0开始的
		if (month < 10){
			month = "0" + month;
		}
		var day = date.getDate();
		if (day < 10){
			day = "0" + day;
		}
		var hour = date.getHours();
		if (hour < 10){
			hour = "0" + hour;
		}
		var min = date.getMinutes();
		if (min < 10){
			min = "0" + min;
		}
		var sec = date.getSeconds();
		if (sec < 10){
			sec = "0" + sec;
		}
		var newTime = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec;
		return newTime;
	}
	function toreply(index) {
		var item = $("#pro_table").datagrid('getRows')[index];
		var title = '帖子详情';
		window.parent.window.addTab(title, "toreply.do?postid=" + item.id, item.id);
	}
	function toperson(index) {
		var item = $("#pro_table").datagrid('getRows')[index];
		var title = '他的帖子';
		window.parent.window.addTab(title, "toperson.do?userid=" + item.createrid, item.createrid);
	}
</script>

</html>